package com.cinsea.ext.xyc;

import com.cinsea.common.*;
import com.cinsea.htmlselect.service.OptionService;
import com.cinsea.interfaces.form.Formdata;
import com.cinsea.interfaces.form.FormdataServiceImpl;
import com.cinsea.interfaces.model.Cell;
import com.cinsea.interfaces.model.Dataset;
import com.cinsea.interfaces.workflow.RequestInfo;
import com.cinsea.util.DateUtil;
import com.cinsea.util.NumberUtil;
import com.cinsea.util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-12-31
 * Time: 下午12:03
 * To change this template use File | Settings | File Templates.
 */
public class MovePx extends BasePX {
    @Override
    public void before(Map context) {
        JdbcDao jdbcDao = Env.getBean("jdbcDao");
        String actiontype = StringUtil.null2String(context.get("actiontype"));
        Map m=(Map)context.get("parameters");
        String fromarea =  StringUtil.null2String(m.get("ut_moveplant_fromarea"));
        String toarea =  StringUtil.null2String(m.get("ut_moveplant_toarea"));
        String lzid = StringUtil.null2String(m.get("ut_moveplant_lvzhi"));
        String hhsl = StringUtil.null2String(m.get("ut_moveplant_amount"),"0");
        String yzdj = StringUtil.null2String(m.get("ut_moveplant_yuezudanjia"),"0");
        if(actiontype.equals(ActionType.SUBMIT_WFPROCESS)){
            OptionService optionService = Env.getBean("optionService");
            Map lvzhidata = jdbcDao.queryForMap("select guigexinghao,chmc,chdlmc,zjldw from ut_plantinfo where processid=?", new Object[]{lzid});
            String chdlmc =StringUtil.null2String(lvzhidata.get("chdlmc"));
            String sq = "";
            if(chdlmc.equals("402881923ff47408013ff54243eb0027")){
                sq = "select processid from ut_postplant where baiwei =? and huajia = ?";
            }
            else if (chdlmc.equals("402881923ff47408013ff54281b50028")){
                sq = "select processid from ut_postplant where baiwei =? and huapen = ?";
            }
            else{
                sq = "select processid from ut_postplant where baiwei =? and lvzhi = ?";
            }
            String apper = StringUtil.null2String(m.get("ut_moveplant_shenqingren"),Env.getRemoteUser().getId());
            String appdate = StringUtil.null2String(m.get("ut_moveplant_shenqingriqi"), DateUtil.getCurrentDate());
            String apptime = StringUtil.null2String(m.get("ut_moveplant_sqsj"),DateUtil.getCurrentTime());
            FormdataServiceImpl si = new FormdataServiceImpl();
            Formdata formdata = new RequestInfo();
            formdata.setOperator(Env.getRemoteUser().getId());
            formdata.setTypeid("402881934060ce6c01406194917b000d"); //换花申请目录id
            Dataset ds = new Dataset();
            List<Cell> mainlist = new ArrayList(); //主表单元格
            Cell c1 = new Cell();
            c1.setId("ut_changeplant_baiwei");
            c1.setValue(fromarea);
            Cell c2 = new Cell();
            c2.setId("ut_changeplant_lvzhi");
            c2.setValue(lzid);
            Map baiweidata = jdbcDao.queryForMap("select lcmc from ut_floorinfo where processid=?", new Object[]{fromarea});
            Cell c3 = new Cell();
            c3.setId("ut_changeplant_bwmc");
            c3.setValue(StringUtil.null2String(baiweidata.get("lcmc")));
            Cell c4 = new Cell();
            c4.setId("ut_changeplant_applier");
            c4.setValue(apper);
            Cell c5 = new Cell();
            c5.setId("ut_changeplant_appdate");
            c5.setValue(appdate);
            Cell c6 = new Cell();
            c6.setId("ut_changeplant_apptime");
            c6.setValue(apptime);
            String type ="402881ee43226cb201435637c4f131ea";//调花
            Cell c7 = new Cell();
            c7.setId("ut_changeplant_hhlx");
            c7.setValue(type);
            Cell c8 = new Cell();
            c8.setId("ut_changeplant_hhzt");
            c8.setValue("402881934061fddd0140622fbc2a0009");//已申请换花
            Cell c10 = new Cell();
            c10.setId("ut_changeplant_tobaiwei");
            c10.setValue(toarea);
            Cell c9 =new Cell();
            c9.setId("ut_changeplant_yzdj");
            c9.setValue(StringUtil.null2String(m.get("ut_moveplant_yuezudanjia"),"0"));
            Cell c11 = new Cell();
            c11.setId("ut_changeplant_hhbz");
            c11.setValue(StringUtil.null2String(m.get("ut_moveplant_beizhu")));

            Cell c12 = new Cell();
            c12.setId("ut_changeplant_guigexinghao");
            c12.setValue(StringUtil.null2String(optionService.get(StringUtil.null2String(lvzhidata.get("guigexinghao"))).getObjname()));
            Cell c13 = new Cell();
            c13.setId("ut_changeplant_lzmc");
            c13.setValue(StringUtil.null2String(lvzhidata.get("chmc")));
            Cell c14 = new Cell();
            c14.setId("ut_changeplant_lzlx");
            c14.setValue(StringUtil.null2String(optionService.get(StringUtil.null2String(lvzhidata.get("chdlmc"))).getObjname()));
            Map baiweidata1 = jdbcDao.queryForMap("select lcmc from ut_floorinfo where processid=?", new Object[]{toarea});
            Cell c15 = new Cell();
            c15.setId("ut_changeplant_tobwmc");
            c15.setValue(StringUtil.null2String(baiweidata1.get("lcmc")));
            Cell c16 = new Cell();
            c16.setId("ut_changeplant_hhsl");
            c16.setValue(hhsl);
            List<Map> list =(List<Map>) jdbcDao.queryForList(sq,new Object[]{toarea,lzid});
            Cell c17 = new Cell();
            c17.setId("ut_changeplant_bwlzid");
            if(list!=null&&list.size()>0){
                Map postplant = list.get(0);
                c17.setValue(StringUtil.null2String(postplant.get("processid")));
            }
            mainlist.add(c1);
            mainlist.add(c2);
            mainlist.add(c3);
            mainlist.add(c4);
            mainlist.add(c5);
            mainlist.add(c6);
            mainlist.add(c7);
            mainlist.add(c8);
            mainlist.add(c9);
            mainlist.add(c10);
            mainlist.add(c11);
            mainlist.add(c12);
            mainlist.add(c13);
            mainlist.add(c14);
            mainlist.add(c15);
            mainlist.add(c16);
            mainlist.add(c17);
            ds.setMaintable(mainlist);
            formdata.setData(ds);
            si.createFormdata(formdata);

          
        }
    }
}
